import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../common/extension/string_extension.dart';
import '../common/api_service.dart';
import '../common/routes.dart';
import '../model/pipe_model.dart';
import '../utils/utils.dart';

class PipeListView extends StatefulWidget {
  final String searchText;

  const PipeListView({super.key, required this.searchText});

  @override
  State<PipeListView> createState() => _PipeListViewState();
}

class _PipeListViewState extends State<PipeListView> {
  final List<PipeModel> _pipeList = [];

  @override
  void initState() {
    super.initState();

    _searchPipeList();
  }

  void _searchPipeList() {
    ApiService().searchPipeList(widget.searchText, 1, (list) {
      setState(() {
        _pipeList.clear();
        _pipeList.addAll(list);
      });
    }, () {});
  }

  Widget _buildListViewItem(BuildContext context, int index) {
    var pipe = _pipeList[index];
    final cover = pipe.cover.getThumbUrl(size: 500);
    final useNum = Utils.formatNumber(pipe.useNum ?? 0);
    final num = "@${pipe.user?.nickname ?? ""}·$useNum互动";

    return InkWell(
        child: Padding(
            padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
            child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(16),
                  boxShadow: const [
                    BoxShadow(
                      color: Color.fromRGBO(0, 0, 0, 0.06),
                      blurRadius: 10,
                      spreadRadius: 2,
                    ),
                  ],
                  border: Border.all(
                    color: const Color.fromRGBO(0, 0, 0, 0.1),
                    width: 0.5,
                  ),
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    ClipRRect(
                        borderRadius: const BorderRadius.only(
                          topLeft: Radius.circular(16),
                          bottomLeft: Radius.circular(16),
                        ),
                        child: CachedNetworkImage(
                          fit: BoxFit.cover,
                          fadeInDuration: const Duration(milliseconds: 100),
                          fadeOutDuration: const Duration(seconds: 0),
                          imageUrl: cover,
                          width: 72,
                          height: 100,
                        )),
                    const SizedBox(width: 10),
                    Expanded(
                        child: SizedBox(
                      height: 100,
                      child: Padding(
                          padding: const EdgeInsets.symmetric(vertical: 6),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(pipe.name, maxLines: 1, style: const TextStyle(fontSize: 15, overflow: TextOverflow.ellipsis, fontWeight: FontWeight.bold)),
                              Text(pipe.summary ?? "", maxLines: 2, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 13)),
                              const SizedBox(height: 5),
                              const Spacer(),
                              Text(num, style: const TextStyle(color: Colors.black45))
                            ],
                          )),
                    ))
                  ],
                ))),
        onTap: () {
          var pipeId = pipe.id ?? "";
          if (pipeId.isNotEmpty) {
            Get.toNamed(Routes.CHAT, arguments: {"pipeId": pipeId});
          } else {
            var cartoonId = pipe.cartoonId ?? "";
            Get.toNamed(Routes.CARTOON_INFO, arguments: {"cartoonId": cartoonId});
          }
        });
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(itemCount: _pipeList.length, itemBuilder: _buildListViewItem);
  }
}
